package com.lib.common.helper.api

import com.lib.framework.manager.ContextManager
import com.lib.framework.manager.LogManager
import com.lib.framework.network.NetworkManager

/**
 * Copyright (C), 2019-2020
 * FileName: ApiHelper
 * Author: Kevin
 * Date: 2020-01-06 18:48
 * Description: api辅助类
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class ApiHelper {

    companion object {

        private const val TAG = "ApiHelper"

        /**
         * OkHttpClient 配置的名称
         */
        const val OKHTTPCLIENT_CONFIG_NAME_HEAD = "Head" // 头部信息配置

        /**
         * 是否初始化
         */
        private var sIsInitNetworkManager = false

        /**
         * 初始化 NetworkManager
         */
        fun initNetworkManager() {
            val networkManager = NetworkManager.getInstance(ContextManager.applicationContext)
            networkManager.mBaseUrl = "http://www.xxx.com"
            networkManager.addOkHttpClientConfig(OKHTTPCLIENT_CONFIG_NAME_HEAD) { _, builder ->
                builder.addInterceptor(HeaderInterceptor())
            }

            networkManager.createRetrofit(true)
            sIsInitNetworkManager = true
        }

        /**
         * 获取 api 所在的服务接口对象
         */
        fun <T> getService(service: Class<T>): T? {
            if (!sIsInitNetworkManager) {
                LogManager.e(TAG, "sIsInitNetworkManager = $sIsInitNetworkManager")
                return null
            }

            return NetworkManager.getInstance(ContextManager.applicationContext).getService(service)
        }
    }
}